package com.example.androidtest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class MyThread extends Thread{
	private Handler handler;
	private URL u ;
//	private String url ="http://www.mengxianyi.net/one/homepage.json";
	String result ;
	public MyThread(Handler handler,String string){
		this.handler = handler;
		try {
			u = new URL(string);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void run() {
		try {
			HttpURLConnection connection = (HttpURLConnection) u.openConnection();
			connection.connect();
			if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
				InputStream is = connection.getInputStream();
				BufferedReader br  = new BufferedReader(new InputStreamReader(is));
				String line = null;
				result = new String();
				
				while((line = br.readLine())!=null){
					result +=line;
				}
				is.close();
				br.close();
				connection.disconnect();
				
				
				
			}
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Message msg = new Message();
		msg.what = 100;
		msg.obj = result;
		handler.sendMessage(msg);
		
	}
	
}
